package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminUserUpdateBO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wei
 * @since 2022/09/06 21:10
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketSearchHistoryMapper historyMapper;


    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketFeedbackMapper feedbackMapper;

    @Override
    public AdminUserListVO queryUserList(int page, int limit, String username, String mobile, String sort, String order) {
        PageHelper.startPage(page, limit);


        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.setOrderByClause(sort + " " + order);
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username != null && "".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && "".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }

        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
//        int num = marketUsers.size();
//        int pages = num / limit + 1;

        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);
        long num = marketUserPageInfo.getTotal();
        int pages = marketUserPageInfo.getPages();
        AdminUserListVO adminUserListVO = new AdminUserListVO();
        adminUserListVO.setPages(pages);
        adminUserListVO.setLimit(limit);
        adminUserListVO.setTotal((int) num);
        adminUserListVO.setPage(page);
        adminUserListVO.setList(marketUsers);


        return adminUserListVO;
    }

    @Override
    public MarketUser queryUserById(int id) {
        MarketUser marketUser = userMapper.selectByPrimaryKey(id);
        return marketUser;
    }

    @Override
    public void userUpdate(AdminUserUpdateBO adminUserUpdateBO) {
        MarketUser marketUser = new MarketUser();

        BeanUtils.copyProperties(adminUserUpdateBO, marketUser);
        Date date = new Date(System.currentTimeMillis());
        marketUser.setUpdateTime(date);


        userMapper.updateByPrimaryKey(marketUser);
    }

    @Override
    public AdminAddressListVO queryAddressList(int page, int limit, String sort, String order, String name, Integer userId) {
        PageHelper.startPage(page, limit);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.setOrderByClause(sort + " " + order);
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
//        int num = marketAddresses.size();
//        int pages = num / limit + 1;
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);
        long num = marketAddressPageInfo.getTotal();
        int pages = marketAddressPageInfo.getPages();
        AdminAddressListVO adminAddressListVO = new AdminAddressListVO();
        adminAddressListVO.setList(marketAddresses);
        adminAddressListVO.setTotal((int) num);
        adminAddressListVO.setPage(page);
        adminAddressListVO.setPages(pages);
        adminAddressListVO.setLimit(limit);
        return adminAddressListVO;
    }

    @Override
    public AdminFootPrintListVO queryFootprintList(int page, int limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.setOrderByClause(sort + " " + order);
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }

        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(marketFootprintExample);
//        int num = marketFootprints.size();
//        int pages = num / limit + 1;
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(marketFootprints);
        int pages = marketFootprintPageInfo.getPages();
        long num = marketFootprintPageInfo.getTotal();
        AdminFootPrintListVO adminFootPrintListVO = new AdminFootPrintListVO();
        adminFootPrintListVO.setList(marketFootprints);
        adminFootPrintListVO.setTotal((int) num);
        adminFootPrintListVO.setLimit(limit);
        adminFootPrintListVO.setPage(page);
        adminFootPrintListVO.setPages(pages);
        return adminFootPrintListVO;
    }

    @Override
    public AdminHistoryListVO queryHistoryList(int page, int limit, String keyword, Integer userId, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketSearchHistory> marketSearchHistories = historyMapper.selectByExample(marketSearchHistoryExample);
//        int num = marketSearchHistories.size();
//        int pages = num / limit + 1;
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);
        int pages = marketSearchHistoryPageInfo.getPages();
        long num = marketSearchHistoryPageInfo.getTotal();

        AdminHistoryListVO adminHistoryListVO = new AdminHistoryListVO();
        adminHistoryListVO.setList(marketSearchHistories);
        adminHistoryListVO.setTotal((int) num);
        adminHistoryListVO.setLimit(limit);
        adminHistoryListVO.setPage(page);
        adminHistoryListVO.setPages(pages);
        return adminHistoryListVO;
    }

    @Override
    public AdminFeedBackListVO queryfeedBackList(int page, int limit, String username, Integer id, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        marketFeedbackExample.setOrderByClause(sort + " " + order);
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> marketFeedbacks = feedbackMapper.selectByExample(marketFeedbackExample);
//        int num = marketFeedbacks.size();
//        int pages = num / limit + 1;
        PageInfo<MarketFeedback> marketFeedbackPageInfo = new PageInfo<>(marketFeedbacks);
        int pages = marketFeedbackPageInfo.getPages();
        long num = marketFeedbackPageInfo.getTotal();
        AdminFeedBackListVO adminFeedBackListVO = new AdminFeedBackListVO();
        adminFeedBackListVO.setList(marketFeedbacks);
        adminFeedBackListVO.setLimit(limit);
        adminFeedBackListVO.setTotal((int) num);
        adminFeedBackListVO.setPage(page);
        adminFeedBackListVO.setPages(pages);
        return adminFeedBackListVO;
    }

    @Override
    public AdminCollectListVo queryCollectList(int page, int limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.setOrderByClause(sort + " " + order);
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
//        int num = marketCollects.size();
//        int pages = num / limit + 1;
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        int pages = marketCollectPageInfo.getPages();
        long num = marketCollectPageInfo.getTotal();
        AdminCollectListVo adminCollectListVo = new AdminCollectListVo();
        adminCollectListVo.setList(marketCollects);
        adminCollectListVo.setLimit(limit);
        adminCollectListVo.setPages((int) num);
        adminCollectListVo.setPage(page);
        adminCollectListVo.setPages(pages);
        return adminCollectListVo;
    }


}

